addEvent("onload", init);
addEvent("onresize", checkOrientation);
var screenmode = 0;

function init(){
    try {
    
        var myStyleTweaks = new StyleTweaker();
        myStyleTweaks.add("Series60/5.0", "styles/tweaks/S605th.css");
        myStyleTweaks.add("Series60/3.2", "styles/tweaks/S603rdFP2.css");
        myStyleTweaks.tweak();
        initCamera();
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

function checkOrientation(){
    try {
        var width = parseInt(screen.width);
        var height = parseInt(screen.height);
        
        //var landscapeTweaker = new StyleTweaker();
        // css not available in templates?
        //landscapeTweaker.add("Series60/5.0", "../resources/styles/tweaks/S605thLandscape.css");
        
        if (width > height) {
            // if already in landscape mode, return
            if (screenmode == 1) 
                return;
            var previewpane = document.getElementById("previewcontainer");
            previewpane.setAttribute("class", "previewcontainerlandscape");
            screenmode = 1;
        }
        else {
            // if already in portrait mode, return
            if (screenmode == 0) 
                return;
            var previewpane = document.getElementById("previewcontainer");
            previewpane.setAttribute("class", "previewcontainerportrait");
            screenmode = 0;
        }
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

// Extracts filename from image url
function getfilenamefromimageurl(imgurl){
    try {
        var pos = imgurl.lastIndexOf("/");
        var linklabel = imgurl.substr(pos + 1);
        return linklabel;
        
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
    
    
}

/*
 Appends new link object containing link to element passed in parameter 'parent'
 */
function appendlinkobject(parent, imgurl){
    try {
        var linklabel = getfilenamefromimageurl(imgurl);
        var a_image = document.createElement("a");
        a_image.setAttribute("href", "#");
        a_image.setAttribute("onclick", "previewImage('" + imgurl + "');");
        a_image.appendChild(document.createTextNode(linklabel));
        var brElement1 = document.createElement("br");
        // assemble all of the various elements
        parent.appendChild(a_image);
        parent.appendChild(brElement1);
        
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
    
}

/*
 This function is called from onCameraStart callback function
 contained in platformservices_20_specific.js.
 Images parameter passed contains an array of image URLs.
 Shows first picture in widget ui preview pane
 and generates linklist to preview pictures if several pictures were taken.
 */
function showImages(images){
    try {
    
        if (images.length > 0) {
            //assign first image in imagesarray to preview pane
            previewImage(images[0]);
            if (images.length > 1) {
                //populate image preview linklist if several pictures taken
                PopulatePicturesList(images);
            }
        }
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
    
}

/*
 onclick handler for start camera button
 */
function onstartCamera_click(){
    try {
        // Start camera
        startCamera();
        // Clear previw pictures link list from possible existing picture links
        ClearPicturesList();
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Clears previw pictures link list
 */
function ClearPicturesList(){
    try {
        var picturelinklist = document.getElementById("imagelist");
        picturelinklist.innerHTML = "";
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Populates linklist to preview pictures
 Images parameter passed contains an array of image URLs.
 */
function PopulatePicturesList(imageurls){
    try {
        // Get paragraph object for taken picture link list
        var pictureurllist = document.getElementById("imagelist");
        // Append imageurls to paragraph
        for (var i = 0; i < imageurls.length; i++) {
            appendlinkobject(pictureurllist, imageurls[i].uri);
        }
        
        
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Loads new picture to image preview pane and adds filename to caption span of preview pane
 */
function previewImage(imgurl){
    try {
        var previewpane = document.getElementById("img_camerapreview");
        previewpane.setAttribute("src", imgurl.uri);
        var pcaption = document.getElementById("previewcaption");
        pcaption.innerHTML = getfilenamefromimageurl(imgurl.uri);
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

